//
//  RMMSetViewController.m
//  Romomo
//
//  Created by puman on 2016/10/18.
//  Copyright © 2016年 mazegong. All rights reserved.
//

#import "RMMSetViewController.h"
#import "RMMMineSetTableViewCell.h"
#import "RMMPersonSetViewController.h"
#import "RMMAboutMineViewController.h"
#import <WebKit/WebKit.h>

@interface RMMSetViewController ()<UITableViewDelegate, UITableViewDataSource>

@end

@implementation RMMSetViewController
{
    UITableView *_tableView;
    NSString *_chace;
    NSString *_version;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kCColor;
    self.navigationItem.title = @"设置";
    [self addVies];
    // Do any additional setup after loading the view.
}
- (void)addVies{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 16, kScreenWideth, 422) style:(UITableViewStylePlain)];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.scrollEnabled= NO;
    [_tableView registerClass:[RMMMineSetTableViewCell class] forCellReuseIdentifier:@"Cell"];
    [self.view addSubview:_tableView];
    _chace = [self getChace];
    _version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    } else if (section == 1) {
        return 4;
    } else {
        return 1;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 54;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 2) {
        return 70;
    }
    return 16;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = kCColor;
    if (section == 2) {
        view.frame = CGRectMake(0, 0, kScreenWideth, 70);
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        button.backgroundColor = kWhiteColor;
        button.frame = CGRectMake(0, 16, kScreenWideth, 54);
        [button setTitle:@"退出登录" forState:(UIControlStateNormal)];
        [button setTitleColor:COLORFROMCODE(0xF95B45, 1) forState:(UIControlStateNormal)];
        button.titleLabel.font = kFont(16);
        BUTTON_S(button, outLanding);
        [view addSubview:button];

    } else {
        view.frame = CGRectMake(0, 0, kScreenWideth, 16);
    }
    return view;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RMMMineSetTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (indexPath.section == 0) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.content.text = @"修改资料";
        cell.titleName.text = @"个人资料";

    } else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            cell.titleName.text = @"订单退改规则";
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        } else if (indexPath.row == 1) {
            cell.titleName.text = @"关于我们";
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        } else if (indexPath.row == 2){
            cell.titleName.text = @"客服电话";
            cell.content.text = @"400-696-7679";
            cell.content.textColor = kBasicColor;
        } else {
            cell.titleName.text = @"版本号";
            cell.content.text = _version;
            cell.content.textColor = kBasicColor;
        }
    } else {
        cell.titleName.text = @"清除缓存";
        cell.content.text =  _chace;
    }
    return cell;
}
- (void)outLanding{
    _exitLanding();
    NS_POST(@"TUICHUDENGLU", nil, nil);
    [[RMMFileManager shareHandle].userDefaults removeObjectForKey:@"CUST_ID"];
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        RMMPersonSetViewController *setVC = [[RMMPersonSetViewController alloc] init];
        [self.navigationController pushViewController:setVC animated:YES];
    } else if (indexPath.section == 1) {
        if (indexPath.row == 2) {
            [RMMFileManager callKeFuWithPhoneNumber:NumberSJ onView:self.view];
        } else if (indexPath.row == 3){
            
            } else {
                RMMAboutMineViewController *VC = [[RMMAboutMineViewController alloc] init];
                if (indexPath.row == 0) {
                    VC.titleName = @"订单退改规则";
                } else {
                    VC.titleName = @"关于我们";
                }
                [self.navigationController pushViewController:VC animated:YES];
            }
    } else {
        CGFloat size = [[SDImageCache sharedImageCache] getSize];
        NSString *sizes = [self fileSizeWithInterge:size];
        NSString *message = nil;
        if ([sizes isEqualToString:@"0B"]) {
            message = @"没有缓存不用清除!";
        } else {
            message = [NSString stringWithFormat:@"共有%@的缓存,主要是图片的缓存,确定要清除吗?",sizes];
        }
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"清除缓存" message:message preferredStyle:(UIAlertControllerStyleAlert)];
        UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确认" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            if ([message isEqualToString:@"没有缓存不用清除!"]) {
                [RMMFileManager MBProgressWith:@"没有缓存!" onView:self.view];
            } else {
                dispatch_async(dispatch_get_global_queue(0, 0), ^{
                    [self deleteWabCache];
                });
                [[SDImageCache sharedImageCache] clearDisk];
                [MMProgressHUD show];
                [MMProgressHUD dismissWithSuccess:@"清除缓存成功!" title:nil afterDelay:1.5];
                _chace = @"0B";
                [_tableView reloadSections:[[NSIndexSet alloc] initWithIndex:2] withRowAnimation:(UITableViewRowAnimationNone)];
        
            }
        }];
        [alertController addAction:actionCancel];
        [alertController addAction:action];
        [self presentViewController:alertController animated:YES completion:nil];
    }
    
}
// 请出H5缓存
- (void)deleteWabCache{
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 9.0) {
        NSSet *websiteDataTypes = [NSSet setWithArray:@[WKWebsiteDataTypeDiskCache,WKWebsiteDataTypeMemoryCache,]];
        NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
        [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
        }];
    } else {
        NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSString *cookiesFolderPath = [libraryPath stringByAppendingString:@"/Cookies"];
        NSError *errors;
        [[NSFileManager defaultManager] removeItemAtPath:cookiesFolderPath error:&errors];
        
    }
}
- (NSString *)getChace{
    CGFloat size = [[SDImageCache sharedImageCache] getSize];
    NSString *sizes = [self fileSizeWithInterge:size];
    return sizes;
}
// 计算缓存大小
- (NSString *)fileSizeWithInterge:(CGFloat)size{
    // 1k = 1024, 1m = 1024k
    if (size < 1024) {// 小于1k
        return [NSString stringWithFormat:@"%.0fB",size];
    }else if (size < 1024 * 1024){// 小于1m
        CGFloat aFloat = size/1024;
        return [NSString stringWithFormat:@"%.0fK",aFloat];
    }else if (size < 1024 * 1024 * 1024){// 小于1G
        CGFloat aFloat = size/(1024 * 1024);
        return [NSString stringWithFormat:@"%.2fM",aFloat];
    }else{
        CGFloat aFloat = size/(1024*1024*1024);
        return [NSString stringWithFormat:@"%.2fG",aFloat];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
